package model

import (
	"time"
	"gorm.io/gorm"
)

type FuelType struct {
	Id         int64     `json:"id" gorm:"primaryKey;autoIncrement"`
	Type       string    `json:"type" gorm:"uniqueIndex;size:20;not null"`
	Name       string    `json:"name" gorm:"size:50;not null"`
	Price      float64   `json:"price" gorm:"type:decimal(10,2);not null"`
	CreateTime time.Time `json:"createTime" gorm:"autoCreateTime"`
	UpdateTime time.Time `json:"updateTime" gorm:"autoUpdateTime"`
}

func (FuelType) TableName() string {
	return "fuel_types"
}

type FuelTypeModel struct {
	db *gorm.DB
}

func NewFuelTypeModel(db *gorm.DB) *FuelTypeModel {
	return &FuelTypeModel{db: db}
}

func (m *FuelTypeModel) FindAll() ([]FuelType, error) {
	var fuelTypes []FuelType
	err := m.db.Find(&fuelTypes).Error
	return fuelTypes, err
}

func (m *FuelTypeModel) UpdatePrice(id int64, price float64) (*FuelType, error) {
	var fuelType FuelType
	err := m.db.Model(&fuelType).Where("id = ?", id).Update("price", price).Error
	if err != nil {
		return nil, err
	}
	
	err = m.db.First(&fuelType, id).Error
	return &fuelType, err
}

func (m *FuelTypeModel) FindById(id int64) (*FuelType, error) {
	var fuelType FuelType
	err := m.db.First(&fuelType, id).Error
	return &fuelType, err
}